package lesson4.example7;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;

public class TestByteBuddy {

    public static void main(String args[]) throws Exception {

        DynamicType.Unloaded<?> dynamicType = new ByteBuddy()
        		// 动态创建的类继承TestDemo类
                .subclass(TestDemo.class)
                // 方法筛选器
                .method(ElementMatchers.named("getProduct"))
                // 加入方法拦截
                .intercept(MethodDelegation.to(Log.class))
                // 触发生成一个新的类
                .make();

        // 加载类
        Class<?> clazz = dynamicType.load(TestByteBuddy.class.getClassLoader()).getLoaded();

        // 反射调用
        clazz.getMethod("getProduct", Long.class, String.class).invoke(clazz.newInstance(), 100000L, "Book");

    }

}
